package com.example.mapping;

import com.example.mapping.entity.process.JsonMappingProcessInfo;
import com.example.mapping.entity.visit.JsonMappingVisitInfo;
import com.example.mapping.entity.JsonMappingSpec;
import com.example.mapping.enumeration.MappingSpecEnum;
import com.example.mapping.manage.processor.JsonFieldProcessor;
import com.example.mapping.manage.visit.JsonTraverseVisitor;
import com.example.mapping.utils.JacksonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.example.mapping.constant.Constants.MappingConstants.MAPPINGSPECDELIMITER;


/**
 * test
 *
 * @author baoyoujia
 * @date 2022/10/16
 */
public class Test {

    public static void main(String[] args) throws JsonProcessingException {
        String reqJsonStr = "{\n" +
                "  \"msg\": \"成功\",\n" +
                "  \"traceID\": \"cc482fe1-7650-4029-a5d0-d5e4283fb7dc\",\n" +
                "  \"code\": \"000\",\n" +
                "  \"data\": {\n" +
                "    \"records\": [\n" +
                "      {\n" +
                "        \"reason\": \"\",\n" +
                "        \"auditBy\": \"\",\n" +
                "        \"suppliers\": \"0\",\n" +
                "        \"allotName\": \"\",\n" +
                "        \"demandType\": \"1\",\n" +
                "        \"quoted\": \"0\",\n" +
                "        \"billExecuteDate\": \"20220824\",\n" +
                "        \"supplierInfos\": [],\n" +
                "        \"billRemark\": \"询价测试\",\n" +
                "        \"action\": \"0\",\n" +
                "        \"shopMallBillID\": \"0\",\n" +
                "        \"billNo\": \"XJ202208240002\",\n" +
                "        \"billCreateBy\": \"鲍友嘉\",\n" +
                "        \"allotType\": \"\",\n" +
                "        \"houseID\": \"0\",\n" +
                "        \"billCategory\": \"0\",\n" +
                "        \"supplierID\": \"0\",\n" +
                "        \"groupID\": \"953\",\n" +
                "        \"billDate\": \"20220824\",\n" +
                "        \"goodsNumTotal\": \"0.00000000\",\n" +
                "        \"taxAmountTotal\": \"0.00000000\",\n" +
                "        \"houseInfos\": [\n" +
                "          {\n" +
                "            \"houseName\": \"dp鲜花店1\",\n" +
                "            \"houseID\": \"5141834\"\n" +
                "          }\n" +
                "        ],\n" +
                "        \"isChecked\": \"0\",\n" +
                "        \"voucherNo\": \"\",\n" +
                "        \"inquirerName\": \"\",\n" +
                "        \"supplierLinkMan\": \"\",\n" +
                "        \"auditTime\": \"0\",\n" +
                "        \"shopID\": \"0\",\n" +
                "        \"extfield1\": \"1\",\n" +
                "        \"supplierIDs\": \"\",\n" +
                "        \"sourceTemplate\": \"\",\n" +
                "        \"priceEndDate\": \"20220824\",\n" +
                "        \"marketInquirers\": \"0\",\n" +
                "        \"shopMallBillNo\": \"\",\n" +
                "        \"actionTime\": \"0\",\n" +
                "        \"totalPrice\": \"0.00000000\",\n" +
                "        \"sourceTemplateID\": \"\",\n" +
                "        \"shopName\": \"\",\n" +
                "        \"finishInquirers\": \"0\",\n" +
                "        \"allotID\": \"0\",\n" +
                "        \"priceStartDate\": \"20220824\",\n" +
                "        \"billCreateTime\": \"20220824145822\",\n" +
                "        \"demandName\": \"\",\n" +
                "        \"houseName\": \"\",\n" +
                "        \"orgCode\": \"\",\n" +
                "        \"billID\": \"2380345\",\n" +
                "        \"mainBillNo\": \"\",\n" +
                "        \"billStatus\": \"1\",\n" +
                "        \"voucherDetailIDs\": \"\",\n" +
                "        \"supplierName\": \"\",\n" +
                "        \"pretaxAmountTotal\": \"0.00000000\",\n" +
                "        \"billType\": \"20\",\n" +
                "        \"mainBillID\": \"0\",\n" +
                "        \"createBy\": \"\",\n" +
                "        \"marketName\": \"\",\n" +
                "        \"demandID\": \"5141841\",\n" +
                "        \"supplierLinkTel\": \"\",\n" +
                "        \"extfield\": \"2\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"reason\": \"\",\n" +
                "        \"auditBy\": \"\",\n" +
                "        \"suppliers\": \"1\",\n" +
                "        \"allotName\": \"\",\n" +
                "        \"demandType\": \"1\",\n" +
                "        \"quoted\": \"1\",\n" +
                "        \"billExecuteDate\": \"20220824\",\n" +
                "        \"supplierInfos\": [],\n" +
                "        \"billRemark\": \"询价测试\",\n" +
                "        \"action\": \"0\",\n" +
                "        \"shopMallBillID\": \"0\",\n" +
                "        \"billNo\": \"XJ202208240001\",\n" +
                "        \"billCreateBy\": \"鲍友嘉\",\n" +
                "        \"allotType\": \"\",\n" +
                "        \"houseID\": \"0\",\n" +
                "        \"billCategory\": \"0\",\n" +
                "        \"supplierID\": \"0\",\n" +
                "        \"groupID\": \"953\",\n" +
                "        \"billDate\": \"20220824\",\n" +
                "        \"goodsNumTotal\": \"0.00000000\",\n" +
                "        \"taxAmountTotal\": \"0.00000000\",\n" +
                "        \"houseInfos\": [\n" +
                "          {\n" +
                "            \"houseName\": \"dp鲜花店1\",\n" +
                "            \"houseID\": \"5141834\"\n" +
                "          }\n" +
                "        ],\n" +
                "        \"isChecked\": \"0\",\n" +
                "        \"voucherNo\": \"\",\n" +
                "        \"inquirerName\": \"\",\n" +
                "        \"supplierLinkMan\": \"\",\n" +
                "        \"auditTime\": \"20220824143034\",\n" +
                "        \"shopID\": \"0\",\n" +
                "        \"extfield1\": \"1\",\n" +
                "        \"supplierIDs\": \"\",\n" +
                "        \"sourceTemplate\": \"\",\n" +
                "        \"priceEndDate\": \"20220824\",\n" +
                "        \"marketInquirers\": \"1\",\n" +
                "        \"shopMallBillNo\": \"\",\n" +
                "        \"actionTime\": \"0\",\n" +
                "        \"totalPrice\": \"0.00000000\",\n" +
                "        \"sourceTemplateID\": \"\",\n" +
                "        \"shopName\": \"\",\n" +
                "        \"finishInquirers\": \"0\",\n" +
                "        \"allotID\": \"0\",\n" +
                "        \"priceStartDate\": \"20220824\",\n" +
                "        \"billCreateTime\": \"20220824143019\",\n" +
                "        \"demandName\": \"\",\n" +
                "        \"houseName\": \"\",\n" +
                "        \"orgCode\": \"\",\n" +
                "        \"billID\": \"2380296\",\n" +
                "        \"mainBillNo\": \"\",\n" +
                "        \"billStatus\": \"3\",\n" +
                "        \"voucherDetailIDs\": \"\",\n" +
                "        \"supplierName\": \"\",\n" +
                "        \"pretaxAmountTotal\": \"0.00000000\",\n" +
                "        \"billType\": \"20\",\n" +
                "        \"mainBillID\": \"0\",\n" +
                "        \"createBy\": \"\",\n" +
                "        \"marketName\": \"\",\n" +
                "        \"demandID\": \"5141841\",\n" +
                "        \"supplierLinkTel\": \"\",\n" +
                "        \"extfield\": \"2\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"pageInfo\": {\n" +
                "      \"total\": \"2\",\n" +
                "      \"pages\": \"1\",\n" +
                "      \"pageSize\": \"25\",\n" +
                "      \"pageNum\": \"1\"\n" +
                "    }\n" +
                "  },\n" +
                "  \"success\": true,\n" +
                "  \"ms\": 82,\n" +
                "  \"host\": \"supplychain-api-v1-65d779969f-jndrl\",\n" +
                "  \"traceMD5\": null\n" +
                "}";
        JsonMappingSpec jsonMappingSpec = new JsonMappingSpec();
        jsonMappingSpec.setMappingAbsolute("billID");
        jsonMappingSpec.setType(MappingSpecEnum.SPEC.getType());
        jsonMappingSpec.setSrc("billID");
        jsonMappingSpec.setDest("voucherNo1");
        // 转成node
        ObjectNode jsonNodes = JacksonUtils.parseObjectNode(reqJsonStr);
        // 访问者信息
        JsonMappingVisitInfo jsonMappingVisitInfo = new JsonMappingVisitInfo();
        jsonMappingVisitInfo.setName(jsonMappingSpec.getMappingAbsolute());
        jsonMappingVisitInfo.setNode(jsonNodes);
        JsonTraverseVisitor visitor = new JsonTraverseVisitor();
        // 执行访问者
        visitor.visit(jsonMappingVisitInfo);
        // 处理器信息
        JsonMappingProcessInfo jsonMappingProcessInfo = new JsonMappingProcessInfo();
        jsonMappingProcessInfo.setNodes(jsonMappingVisitInfo.getNodes());
        jsonMappingProcessInfo.setJsonMappingSpec(jsonMappingSpec);
        // 字段映射处理器
        JsonFieldProcessor processor = new JsonFieldProcessor();
        processor.handle(jsonMappingProcessInfo);

        System.out.println(jsonNodes);
    }
}
